package com.woniu.controller;

import com.woniu.pojo.PageBean;
import com.woniu.pojo.User;
import com.woniu.pojo.Userrole;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("list")
    public String list(){
        return "user/list";
    }
    @RequestMapping("edit")
    public String edit(Integer uid, Model model)
    {
        model.addAttribute("uid",uid);
        return "user/edit";

    }
    @RequestMapping("assignRole")
    public String assignRole(Integer uid, Model model)
    {
        model.addAttribute("uid",uid);
        return "user/assignRole";

    }

    @RequestMapping("assignRoleShowDo")
    @ResponseBody
    public Map<String,Object> assignRoleShowDo(Integer uid){
        Map<String,Object> resutlMap = new HashMap<>();
        try{
            Map<String,Object> assignRoleMap = userService.assignRoleShow(uid);
            resutlMap.put("unassignRoleList",assignRoleMap.get("unassignRoleList"));
            resutlMap.put("assignRoleList",assignRoleMap.get("assignRoleList"));
            resutlMap.put("result",true);
        }catch (Exception e){
            resutlMap.put("result",false);
        }
        return resutlMap;
    }
    @RequestMapping("listDo")
    @ResponseBody
    public Map<String,Object> listDo(PageBean pageBean,User user){
        Map<String,Object> resutlMap = new HashMap<>();
        try{
            List<User> userList = userService.getUsersByPage(pageBean,user);
            resutlMap.put("userList",userList);
            resutlMap.put("pageBean",pageBean);
            resutlMap.put("result",true);
        }catch (Exception e){
            resutlMap.put("result",false);
        }
        return resutlMap;
    }

    @RequestMapping("delSelectedDo")
    @ResponseBody
    public Map<String,Object> delSelectedDo(Integer[] uids){
        Map<String,Object> resutlMap = new HashMap<>();
        try{
            userService.deleteSelected(uids);
            resutlMap.put("result",true);
        }catch (Exception e){
            resutlMap.put("result",false);
        }
        return resutlMap;
    }

    @RequestMapping("selectUserDo")
    @ResponseBody
    public Map<String,Object> selectUserDo(Integer uid){
        Map<String,Object> resutlMap = new HashMap<>();
        try{
            User user = userService.getUserByUid(uid);
            resutlMap.put("user",user);
            resutlMap.put("result",true);
        }catch (Exception e){
            resutlMap.put("result",false);
        }
        return resutlMap;
    }

    @RequestMapping("editDo")
    @ResponseBody
    public Map<String,Object> editDo(User user){
        Map<String,Object> resutlMap = new HashMap<>();
        try{
            userService.editUser(user);
            resutlMap.put("result",true);
        }catch (Exception e){
            resutlMap.put("result",false);
        }
        return resutlMap;
    }
}
